package com.hellogood.yaohuo.server.mgr;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.PsThDetail;
import com.hellogood.yaohuo.shared.entity.PsThForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class PeisongTuihuoMgr {
	Logger log = Logger.getLogger(this.getClass());

	public String createPeisongTuihuoForm(PsThForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPsThFormDAO().createPeisongTuihuo(form, false);
		if (result.equals("")) {
			addLog(form.getPsThBillNo(), false);
		}
		return result;
	}

	public String modifyPeisongTuihuoForm(PsThForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPsThFormDAO().createPeisongTuihuo(form, true);
		if (result.equals("")) {
			addLog(form.getPsThBillNo(), true);
		}
		return result;
	}

	private void execute(PsThForm form) {
		List<PsThDetail> list = form.getList();
		int totalCounts = 0;
		BigDecimal formCost = new BigDecimal(0);
		BigDecimal formTotal = new BigDecimal(0);

		for (PsThDetail d : list) {
			BigDecimal cost = d.getjPrice().multiply(new BigDecimal(d.getCounts()));
			BigDecimal total = d.getPrice().multiply(new BigDecimal(d.getCounts()));
			d.setCost(cost);
			d.setTotal(total);
			totalCounts += d.getCounts();
			formCost = formCost.add(cost);
			formTotal = formTotal.add(total);
		}
		form.setOrgCode("C001");
		form.setVendorCode("C002");
		form.setVendorName("");
		form.setCounts(totalCounts);
		form.setCost(formCost);
		form.setNetCost(formCost);
		form.setTotal(formTotal);

	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改移库单" + billNo);
		} else {
			log.setContent("新建移库单" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public String getLastPsThBillNo(String billNo) {
		return DAOFactory.getInstance().createPsThFormDAO().getLastPsThBillNo(billNo);		
	}

	public List<PsThForm> searchPsThForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("美季")) {
			level = 1;
		}
		if (level == 2) {
			return DAOFactory.getInstance().createPsThFormDAO().searchPsThFormByQuyu(code, startDate, endDate, rz);
		} else if (level == 3) {
			return DAOFactory.getInstance().createPsThFormDAO().searchPsThForm(code, startDate, endDate, rz);
		} else if (level == 1) {
			return DAOFactory.getInstance().createPsThFormDAO().searchPsThFormByQuyu(null, startDate, endDate, rz);
		}
		return null;
	}

	public PsThForm getFormDetail(String billNo) {
		PsThForm form = DAOFactory.getInstance().createPsThFormDAO().getFormByBillNo(billNo);
		List<PsThDetail> list = DAOFactory.getInstance().createPsThDetailDAO().getDetailByBillNo(billNo);
		form.setList(list);
		return form;
	}

	public String deletePeisongTuihuo(String billNo) {
		return DAOFactory.getInstance().createPsThFormDAO().deletePeisongTuihuo(billNo);
	}

	public String rz(List<String> bills) {
		String result = "";
		try {
			for (String bill : bills) {
				DAOFactory.getInstance().createPsThFormDAO().rz(bill);
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			result = e.getMessage();
		}
		return result;
	}
}
